package qc.module.duty.api.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import qc.common.core.exception.QCPromptException;
import qc.module.platform.dto.dept.DeptSimpleDto;
import qc.module.platform.dto.uac.UacUserInfoDto;

import java.util.List;

/**
 * RestTemplate调用Platform接口统一处理类，controller中需要调用platform中的接口均使用此类中的方法
 *
 * @author QuCheng Tech
 * @since 2025/3/4
 */
@Service
public class RestTemplatePlatform {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    /**
     * 根据token获取用户信息
     *
     * @param headers 调用平台接口使用的header信息，传入项目编码和token
     * @return 用户
     * @author QuCheng Tech
     * @since 2024/7/1
     */
    public UacUserInfoDto getUserInfo(HttpHeaders headers) throws QCPromptException {
        List<ServiceInstance> instances = discoveryClient.getInstances("module-platform");
        //如果获取到的微服务的实例数为0，抛出异常提示
        if (instances == null || instances.size() < 0x1)
            throw new QCPromptException("调用module-platform微服务时无任何有效的实例。");

        ServiceInstance service = instances.get(0);
        String url = "http://" + service.getHost() + ":" + service.getPort() + "/invoke/userinfo";

        HttpEntity<String> formEntity = new HttpEntity<String>(null, headers);
        ParameterizedTypeReference<UacUserInfoDto> responseType = new ParameterizedTypeReference<UacUserInfoDto>() {
        };

        ResponseEntity<UacUserInfoDto> response = restTemplate.exchange(
                url,//获取资源的地址
                HttpMethod.GET,
                formEntity,
                responseType
        );
        UacUserInfoDto resultDto = response.getBody();

        return resultDto;
    }

    /***
     * 获取当前用户关联的部门ID集合（获取用户直接关联的部门ID集合，不包含关联部门的下级和子级部门）；用户和项目编码均从header中获取
     *
     * @param headers 请求头
     * @return java.util.List<java.lang.Integer>
     * @author QuCheng Tech
     * @since 2025/3/4
     */
    public List<Integer> getUserRelatedDeptIds(HttpHeaders headers) throws QCPromptException {
        List<ServiceInstance> instances = discoveryClient.getInstances("module-platform");
        //如果获取到的微服务的实例数为0，抛出异常提示
        if (instances == null || instances.size() < 0x1)
            throw new QCPromptException("调用module-platform微服务时无任何有效的实例。");

        ServiceInstance service = instances.get(0);
        String url = "http://" + service.getHost() + ":" + service.getPort() + "/invoke/userrelatedeptids";

        HttpEntity<String> formEntity = new HttpEntity<String>(null, headers);
        ParameterizedTypeReference<List<Integer>> responseType = new ParameterizedTypeReference<List<Integer>>() {
        };

        ResponseEntity<List<Integer>> response = restTemplate.exchange(
                url,//获取资源的地址
                HttpMethod.GET,
                formEntity,
                responseType
        );
        return response.getBody();
    }

    /**
     * 获取指定项目中的所有部门List；通过参数指定项目编码；返回的部门信息中包含部门层级序号，用于调用方需要设置数据权限控制的部门列表时使用
     *
     * @param projectCode 项目编码
     * @return 部门简要信息集合
     * @author QuCheng Tech
     * @since 2024/5/6
     */
    public List<DeptSimpleDto> getProjectDeptList(String projectCode) throws QCPromptException {
        List<ServiceInstance> instances = discoveryClient.getInstances("module-platform");
        //如果获取到的微服务的实例数为0，抛出异常提示
        if (instances == null || instances.size() < 0x1)
            throw new QCPromptException("调用module-platform微服务时无任何有效的实例。");

        ServiceInstance service = instances.get(0);
        String url = "http://" + service.getHost() + ":" + service.getPort() + "/invoke/projectdeptlist/" + projectCode;

        HttpEntity<String> formEntity = new HttpEntity<String>(null, null);
        ParameterizedTypeReference<List<DeptSimpleDto>> responseType = new ParameterizedTypeReference<List<DeptSimpleDto>>() {
        };

        ResponseEntity<List<DeptSimpleDto>> response = restTemplate.exchange(
                url,//获取资源的地址
                HttpMethod.GET,
                formEntity,
                responseType
        );
        List<DeptSimpleDto> resultDto = response.getBody();
        return resultDto;
    }
}
